<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>欢迎使用校园二手交易系统</title>

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
            href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
            rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

</head>
<script type="text/javascript" src="../js/jquery.min.js" ></script>
<script>
</script>
<body id="page-top" onload="init()">

<div id="wrapper">

    <!-- Sidebar -->
    <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

        <a class="sidebar-brand d-flex align-items-center justify-content-center" href="main.html">
            <div class="sidebar-brand-icon rotate-n-15">
                <i class="fas fa-laptop-house"></i>
            </div>
            <div class="sidebar-brand-text mx-3" style="font-size: 19px;">校园二手交易</div>
        </a>

        <hr class="sidebar-divider my-0">

        <!-- 返回 -->
        <li class="nav-item">
            <a class="nav-link" href="javascript:history.back()">
                <i style="font-size: 18px" class="fas fa-fw fa-arrow-left"></i>
                <span style="font-size: 20px; font-weight: bold">返回</span>
            </a>
        </li>

        <!-- Divider -->
        <hr class="sidebar-divider d-none d-md-block">

        <!-- Sidebar Toggler (Sidebar) -->
        <div class="text-center d-none d-md-inline">
            <button class="rounded-circle border-0" id="sidebarToggle"></button>
        </div>

    </ul>
    <!-- End of Sidebar -->

    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">

        <!-- Main Content -->
        <div id="content">

            <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
                <div style="margin-left: 15px">
                    请您修改相应的个人信息
                </div>
                <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                    <i class="fa fa-bars"></i>
                </button>

                <!-- Topbar Navbar -->
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item dropdown no-arrow">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <span class="mr-2 d-none d-lg-inline text-gray-600 small">
                                        欢迎您，
                                        <input type="text" id="wecomeName" style="width: 80px; border-style: none" disabled>
                                </span>
                                <img class="img-profile rounded-circle" id="sculpture"
                                 src="img/undraw_profile.svg">
                        </a>
                        <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in"
                             aria-labelledby="userDropdown">

                            <a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">
                                <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
                                退出登录
                            </a>
                        </div>
                    </li>

                </ul>

            </nav>


            <form id="form1" onsubmit="return false" action="##" style="margin-left: 20px">
                头像:&ensp;&ensp;&ensp;&ensp;&ensp;<img id="photoShow" alt="头像" width="200" height="200"/><br>
                <br>
                <input type="file" id="file">
                <input type="submit" value="上传" onclick="uploadFile()" class="btn btn-primary"><br>
                <br>
                <input type="hidden" id="id" name="id">
                <input type="hidden" id="photo" name="photo">
                用户名:&ensp;&ensp;&ensp;<input type="text" id="userName"></br>
                <br>
                城市名:&ensp;&ensp;&ensp;<select id="cityId" onchange="schoolList()">
                <option value=''>请选择</option>
                </select></br>
                <br>
                学校名:&ensp;&ensp;&ensp;<select id="schoolId" onchange="selectSchool(value)">
                <option value=''>请选择</option>
                </select></br>
                <br>
                学号:&ensp;&ensp;&ensp;&ensp;&ensp;<input type="text" id="studentId"></br>
                <br>
                <input type="button" value="确认" onclick="update()" style="text-align: center; font-size: 18px; width: 80px" class="btn btn-primary">
                &ensp;&ensp;&ensp;&ensp;
                <a href="userInfo.html" style="text-align: center; font-size: 18px; width: 80px" class="btn btn-secondary">取消</a>
            </form>

        </div>

        <footer class="sticky-footer bg-white">
            <div class="container my-auto">
                <div class="copyright text-center my-auto">
                    <span>致 力 于 打 造 最 方 便 安 全 的 校 园 二 手 交 易 服 务</span>
                </div>
            </div>
        </footer>

    </div>

</div>


<!-- Logout Modal-->
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
     aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">确定要退出登录？</h5>
                <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">点击‘退出’后，退出系统...</div>
            <div class="modal-footer">
                <button class="btn btn-secondary" type="button" data-dismiss="modal">取消</button>
                <a class="btn btn-primary" href="login.html">退出</a>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap core JavaScript-->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>

<!-- Custom scripts for all pages-->
<script src="js/sb-admin-2.min.js"></script>

<!-- Page level plugins -->
<script src="vendor/chart.js/Chart.min.js"></script>

<!-- Page level custom scripts -->
<script src="js/demo/chart-area-demo.js"></script>
<script src="js/demo/chart-pie-demo.js"></script>

</body>

<script>
    userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
    $("#sculpture").attr("src", userInfo.photo);  // 设置头像
    document.getElementById('wecomeName').value = userInfo.userName; // 设置用户名
    function init() { // 初始化用户数据
        $.ajax({ // 获取城市列表
            type : "GET",
            async: true,
            url : "user/user-server/listCity",
            headers: { // token
                'Authorization' : userInfo.token
            },
            success : function(result) {
                if (result!=null) {
                    var data = result.data;
                    var sel = document.getElementById("cityId");//获取下拉选对象
                    sel.options.length = 1; //清空
                    for(var i=0,l=data.length;i<l;i++){
                        var name = data[i].cityName;
                        var id = data[i].id;
                        var option = new Option(name,id);
                        sel.options.add(option);
                    }
                    return;
                }
            }
        })

        $.ajax({ // 获取学校列表
            type : "GET",
            async: true,
            url : "user/user-server/listSchool",
            headers: { // token
                'Authorization' : userInfo.token
            },
            success : function(result) {
                if (result!=null) {
                    var data = result.data;
                    var sel = document.getElementById("schoolId"); //获取下拉选对象
                    sel.options.length = 1; //清空
                    for(var i=0,l=data.length;i<l;i++){
                        var name = data[i].schoolName;
                        var id = data[i].id;
                        var option = new Option(name,id);
                        sel.options.add(option);
                    }
                }
            }
        })

        $.ajax({ // 获取用户信息
            type : "GET",
            async: true,
            url : "user/user-server/getUser",
            headers: { // token
                'Authorization' : userInfo.token
            },
            dataType : "json",        //返回数据形式为json
            data: {"id": userInfo.userId},
            success : function(result) {
                var data = result.data;
                document.getElementById('id').value = data.id;
                document.getElementById('userName').value = data.userName;
                document.getElementById('studentId').value = data.studentId;
                document.getElementById('photo').value = data.photo;
                $("#photoShow").attr("src",data.photo);
                var cityList = document.getElementById('cityId');
                for(var i=0;i<cityList.options.length;i++){
                    if (cityList.options[i].value == data.cityId) {
                        cityList.options[i].selected = true;
                        break;
                    }
                }
                var schoolList = document.getElementById('schoolId');
                for(var i=0;i<schoolList.options.length;i++){
                    if (schoolList.options[i].value == data.schoolId) {
                        schoolList.options[i].selected = true;
                        break;
                    }
                }
            }
        })
    }


    function update() {
        var userInfoVO={
            id :  $("#id").val(),
            userName :  $("#userName").val(),
            cityId : $("#cityId").val(),
            schoolId : $("#schoolId").val(),
            studentId :  $("#studentId").val(),
            photo : $("#photo").val(),
            token : userInfo.token
        }
        $.ajax({
            url: 'user/user-server/updateUser',
            method: 'PUT',
            headers: { // token
                'Authorization' : userInfo.token
            },
            dataType: 'json',
            contentType : 'application/json; charset=UTF-8',
            data: JSON.stringify(userInfoVO),
            success: function (result) {
                console.log(result);//打印服务端返回的数据(调试用)
                if (result.status == 200) {
                    alert("修改成功");
                    sessionStorage.removeItem("userInfo");
                    // 转为json保存在session loginVO
                    sessionStorage.setItem("userInfo", JSON.stringify(result.data));
                    window.location.href = 'userInfo.html';
                }
                else{
                    alert(result.msg);
                }
            }
        })
    }

    function schoolList() { // 根据城市筛选出学校
        var cityId = $("#cityId").val();
        $.ajax({
            type : "GET",
            async: true,
            url : "user/user-server/listSchool",
            headers: { // token
                'Authorization' : userInfo.token
            },
            dataType : "json",        //返回数据形式为json
            data: {"cityId": cityId},
            success : function(result) {
                if (result!=null) {
                    var data = result.data;
                    var sel = document.getElementById("schoolId"); //获取下拉选对象
                    sel.options.length = 1; //清空
                    for(var i=0,l=data.length;i<l;i++){
                        var name = data[i].schoolName;
                        var id = data[i].id;
                        var option = new Option(name,id);
                        sel.options.add(option);
                    }
                }
            }
        })
    }
    function selectSchool(id) { // 根据学校的城市选择城市
        var obj = document.getElementById('cityId');
        $.ajax({
            type : "GET",
            async: true,
            url : "user/user-server/getSchool",
            headers: { // token
                'Authorization' : userInfo.token
            },
            dataType : "json",        //返回数据形式为json
            data: {"id": id},
            success : function(result) {
                if (result!=null) {
                    var data = result.data;
                    for(var i=0;i<obj.options.length;i++){
                        if (obj.options[i].value == data.cityId) {
                            obj.options[i].selected = true;
                            break;
                        }
                    }
                }
            }
        })
    }

    function uploadFile() {
        var file = $("#file")[0].files[0];
        var formData = new FormData();
        formData.append("file", file);
        $.ajax({
            type:"post",
            url:"user/upload",
            headers: { // token
                'Authorization' : userInfo.token
            },
            processData:false,
            contentType:false,
            data:formData,
            success:function (result) {
                if (result.status == 200) {
                    alert("上传成功");
                    $("#photoShow").attr("src",result.data);
                    document.getElementById('photo').value = result.data;
                }
                else{
                    alert(result.msg);
                }
            }
        })
    }
</script>

</html>